package com.op.studyingliteapp.database.dao;

import io.netty.handler.codec.ByteToMessageDecoder.Cumulator;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.op.studyingliteapp.bean.BaseBean;
import com.op.studyingliteapp.bean.CourseNotifBean;
import com.op.studyingliteapp.bean.NoteBean;
import com.op.studyingliteapp.bean.UserBean;
import com.op.studyingliteapp.database.table.BaseColumn;
import com.op.studyingliteapp.database.table.CourseNotifTable;
import com.op.studyingliteapp.database.table.NoteTable;
import com.op.studyingliteapp.database.table.UserTable;
import com.op.util.StringUtils;

/**
 * @author handsome
 * @version 创建时间：2015年7月25日 下午8:29:20 类说明
 */
public class NoteDao extends AbstractDao {

	public NoteDao(Context context, String userId) {
		super(context, userId);
		this.tb = NoteTable.TABLE_COURSE_NOTE;
	}

	/**
	 * 通过课程ID和userId来获得该用户在该课程所有笔记
	 */
	public List<NoteBean> getNotifByCourseIdAndUserId(String courseId, String userId) {
		List<NoteBean> noteList = new ArrayList<NoteBean>();
		// Cursor cursor = db.rawQuery("select * from" + NoteTable.PROJECTION +
		// "where NoteTable.COLUMN_COURSE_ID=? and NoteTable.COLUMN_USER_ID = ?",
		// new String[] { courseId, userId });
		String where = NoteTable.COLUMN_COURSE_ID + " = ? and " + NoteTable.COLUMN_USER_ID + " = ?";
		Cursor cursor = db.query(NoteTable.TABLE_COURSE_NOTE, NoteTable.PROJECTION, where, new String[] { courseId, userId }, null, null, null);
		while (cursor.moveToNext()) {
			noteList.add(this.makeCourseNotifBeanByCursor(cursor));
		}
		return noteList;
	}

	/**
	 * 通过用户ID来获得该用户所有笔记
	 */
	public List<NoteBean> getNotifByUserId(String userId) {
		List<NoteBean> noteList = new ArrayList<NoteBean>();

		Cursor cursor = db.query(this.tb, NoteTable.PROJECTION, NoteTable.COLUMN_USER_ID + "=" + StringUtils.wrapQuote(userId), null, null, null, null);
		while (cursor.moveToNext()) {
			noteList.add(this.makeCourseNotifBeanByCursor(cursor));
		}

		return noteList;
	}

	/**
	 * 通过笔记ID来删除该笔记
	 */
	public void deleteNotifBynoteId(String noteId) {
		List<NoteBean> noteList = new ArrayList<NoteBean>();
		String where = NoteTable.COLUMN_ID + " = ?";
		this.db.delete(this.tb, where, new String[] { noteId });
	}

	/**
	 * 辅助类，用于生成实例
	 */
	public NoteBean makeCourseNotifBeanByCursor(Cursor cursor) {
		NoteBean noteBean = new NoteBean();

		noteBean.setId(cursor.getString(cursor.getColumnIndex(NoteTable.COLUMN_ID)));
		noteBean.setCreateTime(cursor.getString(cursor.getColumnIndex(NoteTable.COLUMN_CREATE_TIME)));
		noteBean.setUpdateTime(cursor.getString(cursor.getColumnIndex(NoteTable.COLUMN_UPDATE_TIME)));
		noteBean.setContent(cursor.getString(cursor.getColumnIndex(NoteTable.COLUMN_CONTENT)));
		noteBean.setCourseId(cursor.getString(cursor.getColumnIndex(NoteTable.COLUMN_COURSE_ID)));
		noteBean.setCourseName(cursor.getString(cursor.getColumnIndex(NoteTable.COLUMN_COURSE_NAME)));
		// UserBean
		UserBean userBean = new UserBean();
		userBean.setId(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_ID))));
		userBean.setName(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_NAME))));
		userBean.setEmail(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_EMAIL))));
		userBean.setImageUrl(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_IMAGE_URL))));
		userBean.setNumber(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_NUMBER))));
		userBean.setPhone(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_PHONE))));
		userBean.setIsUsing(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_IS_USING))));
		userBean.setType(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER, UserTable.COLUMN_TYPE))));
		noteBean.setUserBean(userBean);
		return noteBean;
	}
}
